package com.sykj.why.mongo.service.impl;

import com.sykj.why.exception.WhyException;
import com.sykj.why.mongo.dao.ProjectTDao;
import com.sykj.why.mongo.document.entity.ProjectT;
import com.sykj.why.mongo.document.entity.RelevantCertificateT;
import com.sykj.why.mongo.document.entity.SuccessorT;
import com.sykj.why.mongo.dto.PageDTO;
import com.sykj.why.mongo.enums.AuditStatusEnum;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.ProjectTService;
import com.sykj.why.mongo.vo.entity.ProjectTVo;
import com.sykj.why.mongo.vo.entity.SuccessorTVo;
import com.sykj.why.util.BaseTools;
import com.sykj.why.util.DateTool;
import com.sykj.why.util.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("ProjectTService")
public class ProjectTServiceImpl extends BasicServiceImpl<ProjectT> implements ProjectTService {

    @Autowired
    private ProjectTDao projectTDao;

    /**
     * 添加非遗项目
     * @param projectT
     */
    @Override
    public void saveProject(ProjectT projectT) {
        //验证非遗项目名称是否重复
        verifyName(projectT);
        //排序
        long count = projectTDao.count(ProjectT.class, false);
        projectT.setSort((int) count);

        //默认未审核状态
        projectT.setAuditStatus(AuditStatusEnum.ApplyStatus.PENDING_REVIEW.getState());

        //设置时间
        projectT.setCreateTime(DateTool.getSystemTime());
        projectT.setUpdateTime(DateTool.getSystemTime());
        //保存
        projectTDao.save(projectT);
    }

    /**
     * 删除非遗项目
     * @param id
     */
    @Override
    public void removeProject(String id) {
        //通过Id查询
        ProjectT projectT = findById(id);
        //逻辑删除
        projectTDao.remove(projectT);
    }

    /**
     * 修改非遗项目
     * @param projectT
     */
    @Override
    public void updateProject(ProjectT projectT) {
        //通过id查询
        ProjectT project = findById(projectT.getId());
        //重新赋值
        project.setProjectName(projectT.getProjectName());
        project.setPedigree(projectT.getPedigree());
        project.setProvince(projectT.getProvince());
        project.setCity(projectT.getCity());
        project.setDistrict(projectT.getDistrict());
        project.setAddress(projectT.getAddress());
        project.setProjectIntro(projectT.getProjectIntro());
        project.setUpdateTime(DateTool.getSystemTime());
        //保存或修改
        projectTDao.saveOrupdate(project);
    }

    /**
     * 启用/禁用非遗项目
     * @param id
     * @param flag
     */
    @Override
    public void disableOrEnable(String id, boolean flag) {
        //通过id查询
        ProjectT projectT= findById(id);
        //修改状态
        projectTDao.disableOrEnable(projectT,flag);
    }

    /**
     * 验证非遗项目名称是否重复
     * @param projectT
     */
    @Override
    public void verifyName(ProjectT projectT) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").ne(projectT.getId()).
                and("projectName").is(projectT.getProjectName()).and("removed").is(false));
        ProjectT project = projectTDao.findOne(query,ProjectT.class);
        if (project != null) {
            throw new WhyException(ResultEnum.REPEAT);
        }
    }

    /**
     * 通过id查询非遗项目
     * @param id
     * @return
     */
    @Override
    public ProjectT findById(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id).and("removed").is(false));
        ProjectT projectT = projectTDao.findOne(query,ProjectT.class);
        if (projectT!=null){
            return projectT;
        }else {
            throw new WhyException(ResultEnum.OBJISNULL);
        }
    }

    /**
     * 查询所有非遗项目
     * @return
     */
    @Override
    public List<ProjectT> findAll() {
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false));
        List<ProjectT> list = projectTDao.findAll(query,ProjectT.class);
        return list;
    }

    /**
     * 分页查询非遗项目
     * @param pageDTO
     * @return
     */
    @Override
    public Page findByPage(PageDTO pageDTO) {
        //查询未删除的传承项目，并分页传给视图
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false));
        long count = projectTDao.count(query, ProjectT.class);
        List<ProjectTVo> list = new ArrayList<>();
        for (ProjectT projectT : projectTDao.findByPage(query, pageDTO, ProjectT.class)) {
            ProjectTVo vo = new ProjectTVo();
            vo = setVo(projectT,vo);
            list.add(vo);
        }
        return new Page(pageDTO, count, list);
    }

    /**
     * 查询待审核传承项目
     *
     * @param pageDTO
     * @return
     */
    @Override
    public Page findCheckByPage(PageDTO pageDTO) {
        Query query = new Query(Criteria.where("removed").is(false).
                and("AuditStatus").is(AuditStatusEnum.ApplyStatus.PENDING_REVIEW.getState()));
        long count = projectTDao.count(query, ProjectT.class);
        List<ProjectT> list = projectTDao.findByPage(
                query.with(new Sort(Sort.Direction.DESC, "updateTime", "createTime")),
                pageDTO, ProjectT.class);
        List<ProjectTVo> voList = new ArrayList<>();
        if (count > 0) {
            for (ProjectT projectT : list) {
                ProjectTVo vo = new ProjectTVo();
                vo = setVo(projectT,vo);
                voList.add(vo);
            }
        }
        return new Page(pageDTO, count, voList);
    }

    public ProjectTVo setVo(ProjectT projectT,ProjectTVo vo){
        BeanUtils.copyProperties(projectT, vo);
        vo.setStatusName(AuditStatusEnum.ApplyStatus.getName(projectT.getAuditStatus()));
        vo.setCreateTime(BaseTools.longToString(projectT.getCreateTime()));
        vo.setUpdateTime(BaseTools.longToString(projectT.getUpdateTime()));
        return vo;
    }

    /**
     * 传承项目审核
     *
     * @param id
     * @param flag
     * @param checkInfo
     */
    @Override
    public void check(String id, boolean flag, String checkInfo) {
        if (StringUtils.isBlank(id)) {
            throw new WhyException(ResultEnum.IDISNULL);
        }
        ProjectT projectT = projectTDao.get(ProjectT.class, id, false);
        if (projectT == null) {
            throw new WhyException(ResultEnum.OBJISNULL);
        }
        if (flag) {
            // 审核通过
            projectT.setAuditStatus(AuditStatusEnum.ApplyStatus.PASS.getState());
        } else {
            // 审核不通过
            projectT.setAuditStatus(AuditStatusEnum.ApplyStatus.NO_PASS.getState());
            projectT.setReason(checkInfo);
        }
        projectTDao.saveOrupdate(projectT);
    }

}
